package com.chen.blue.service.security;

import com.chen.blue.domain.model.system.LoginUser;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created By CLT On 2024/5/1.
 */
@SuppressWarnings("all")
public class SecurityService {

    /**
     * 竞赛活动表相关
     *
     * @return
     */
    public static List<String> creatorsActiveOwnDept() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        List<String> userNames = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object roleList = data.get("creators_active_own_dept");
            if (roleList instanceof List) {
                userNames = (List<String>) roleList;
            }
        }
        return userNames;

    }

    public static List<String> creatorsActiveChildDept() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        List<String> userNames = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object roleList = data.get("creators_active_child_dept");
            if (roleList instanceof List) {
                userNames = (List<String>) roleList;
            }
        }
        return userNames;

    }


    public static List<String> creatorsActiveSelectDept() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        List<String> userNames = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object roleList = data.get("creators_active_select_dept");
            if (roleList instanceof List) {
                userNames = (List<String>) roleList;
            }
        }
        return userNames;

    }


    /**
     * 获取当前用户信息
     *
     * @return
     */
    public static LoginUser currentUser() {

        Authentication authentication = getAuthentication();
        if (Objects.isNull(authentication)) {
            return null;
        }
        Object principal = authentication.getPrincipal();
        LoginUser loginUser = null;
        Map<String, Object> data = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object loginInfo = data.get("loginInfo");
            if (loginInfo instanceof LoginUser) {
                loginUser = (LoginUser) loginInfo;
            }
            if (Objects.nonNull(loginUser)) {
                loginUser.setUsername(currentUserName());
            }
        }
        return loginUser;

    }

    /**
     * 获取当前用户的所在部门及其子部门的id
     *
     * @return
     */
    public static Set<Long> deptChildrenIds() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        Set<Long> deptIds = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object deptChildrenIds = data.get("deptChildrenIds");
            if (deptChildrenIds instanceof Set) {
                deptIds = (Set<Long>) deptChildrenIds;
            }
        }
        return deptIds;

    }

    /**
     * 获取当前用户的自定义部门
     *
     * @return
     */
    public static Set<Long> selectedDeptIds() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        Set<Long> deptIds = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object deptChildrenIds = data.get("selectedDeptIds");
            if (deptChildrenIds instanceof Set) {
                deptIds = (Set<Long>) deptChildrenIds;
            }
        }
        return deptIds;

    }

    /**
     * 当前用户所拥有的菜单并集
     *
     * @return
     */
    public static Set<Long> currentMenuIds() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        Set<Long> menuIds = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object deptChildrenIds = data.get("menuIds");
            if (deptChildrenIds instanceof Set) {
                menuIds = (Set<Long>) deptChildrenIds;
            }
        }
        return menuIds;

    }

    /**
     * 当前的用户拥有的角色集合
     *
     * @return
     */
    public static List<String> currentUserRoles() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        List<String> roleCodes = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object roleList = data.get("userRoles");
            if (roleList instanceof List) {
                roleCodes = (List<String>) roleList;
            }
        }
        return roleCodes;

    }

    /**
     * 获得当前用户的账号名
     *
     * @return
     */
    public static String currentUserName() {

        Authentication authentication = getAuthentication();
        if (Objects.isNull(authentication)) {
            return null;
        }

        Map<String, Object> data = null;
        LoginUser loginUser = null;
        String username = null;
        if (authentication.getPrincipal() instanceof Map) {
            data = (Map<String, Object>) authentication.getPrincipal();

            Object loginInfo = data.get("loginInfo");
            if (loginInfo instanceof LoginUser) {
                loginUser = (LoginUser) loginInfo;
            }
            if (Objects.nonNull(loginUser)) {
                username = loginUser.getUsername();
            }
        }

        return username;

    }

    /**
     * 返回指定DeptId下的所有用户名
     *
     * @return
     */
    public static List<String> getUserNameByDeptId() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        List<String> userNames = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object roleList = data.get("creators");
            if (roleList instanceof List) {
                userNames = (List<String>) roleList;
            }
        }
        return userNames;

    }

    /**
     * 返回选定的DeptId下的所有用户
     *
     * @return
     */
    public static List<String> getUserNameBySelectDeptId() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        List<String> userNames = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object roleList = data.get("creatorsSelect");
            if (roleList instanceof List) {
                userNames = (List<String>) roleList;
            }
        }
        return userNames;

    }

    /**
     * 返回当前用户部门的所有用户
     *
     * @return
     */
    public static List<String> getUserNameByOwn() {

        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        Map<String, Object> data = null;
        List<String> userNames = null;
        if (principal instanceof Map) {
            data = (Map<String, Object>) principal;
            Object roleList = data.get("creatorsCurrent");
            if (roleList instanceof List) {
                userNames = (List<String>) roleList;
            }
        }
        return userNames;

    }

    /**
     * 获得认证信息
     *
     * @return
     */
    private static Authentication getAuthentication() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            return null;
        }
        return context.getAuthentication();
    }


    /**
     * 设置当前用户信息
     *
     * @param loginUser
     * @param request
     */
    public static void setAuthentication(Map<String, Object> data, Collection<? extends GrantedAuthority> authorities, HttpServletRequest request) {
        Authentication authentication = buildAuthentication(data, authorities, request);
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }


    private static Authentication buildAuthentication(Map<String, Object> data, Collection<? extends GrantedAuthority> authorities, HttpServletRequest request) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(data, null, authorities);
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        return authenticationToken;
    }

}
